package com.example.finalwork.Income;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.example.finalwork.DAO.IncomeDAO;
import com.example.finalwork.Entity.Income;
import com.example.finalwork.LoginActivity;
import com.example.finalwork.R;
import com.example.finalwork.lmpl.IncomeDAOlmpl;

import java.util.List;

import static org.junit.Assert.assertNotEquals;

public class IncomeUpdateActivity extends AppCompatActivity implements View.OnClickListener{
    private static final String TAG = "IncomeUpdateActivity";
    private TextView incomeupdate_id,incomeupdate_name,incomeupdate_date,incomeupdate_amount,incomeupdate_remark;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_incomeupdate);
        findViewById(R.id.incomeupdate_query).setOnClickListener(this);
        findViewById(R.id.incomeupdate).setOnClickListener(this);
        findViewById(R.id.incomeupdate_exit).setOnClickListener(this);

        incomeupdate_id = findViewById(R.id.incomeupdate_id);;
        incomeupdate_name = findViewById(R.id.incomeupdate_name);
        incomeupdate_date = findViewById(R.id.incomeupdate_date);
        incomeupdate_amount = findViewById(R.id.incomeupdate_amount);
        incomeupdate_remark = findViewById(R.id.incomeupdate_remark);

        setTitle("更新收入项目功能");

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.incomeupdate_query:
                IncomeDAO incomeDAO = new IncomeDAOlmpl(this);
                List<Income> incomes = incomeDAO.GetAllIncome();
                for(int i = 0 ; i < incomes.size() ; i++)
                {
                    Income income = incomes.get(i);
                    if(income.getIn_id() == Integer.parseInt(incomeupdate_id.getText().toString()) && income.getIn_User().equals(LoginActivity.data))
                    {
                        incomeupdate_name.setText(income.getIn_Name());
                        incomeupdate_date.setText(income.getIn_Date());
                        incomeupdate_amount.setText(income.getIn_Amount());
                        incomeupdate_remark.setText(income.getIn_Remark());
                        Toast.makeText(this,"查询成功！",Toast.LENGTH_SHORT).show();
                        return;
                    }
                }
                Toast.makeText(this,"无查询结果！",Toast.LENGTH_SHORT).show();
                break;
            case R.id.incomeupdate:
                IncomeDAO incomeDAO2 = new IncomeDAOlmpl(this);
                List<Income> incomes2 = incomeDAO2.GetAllIncome();
                for(int i = 0 ; i < incomes2.size() ; i++)
                {
                    Income income2 = incomes2.get(i);
                    if(income2.getIn_id() == Integer.parseInt(incomeupdate_id.getText().toString()) && income2.getIn_User().equals(LoginActivity.data))
                    {
                        income2.setIn_id(Integer.parseInt(incomeupdate_id.getText().toString()));
                        income2.setIn_User(LoginActivity.data);
                        income2.setIn_Name(incomeupdate_name.getText().toString());
                        income2.setIn_Date(incomeupdate_date.getText().toString());
                        income2.setIn_Amount(incomeupdate_amount.getText().toString());
                        income2.setIn_Remark(incomeupdate_remark.getText().toString());
                        int result = incomeDAO2.updateIncome(income2);
                        Log.d(TAG,"update user result - >" + result);
                        assertNotEquals(-1,result);
                        Toast.makeText(this,"更新成功！返回上一级",Toast.LENGTH_SHORT).show();
                        startActivity(new Intent(this,IncomeMainActivity.class));
                        return;
                    }
                }
                Toast.makeText(this,"更新失败，请先进行查询操作！",Toast.LENGTH_SHORT).show();
                break;
            case R.id.incomeupdate_exit:
                Toast.makeText(this,"返回上一级",Toast.LENGTH_SHORT).show();
                startActivity(new Intent(this,IncomeMainActivity.class));
                break;
        }
    }
}
